﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.DataAccess.Engine.Service;

namespace T.FAS.Runtime.DataAccess.Engine
{
    /// <summary>
    /// 数据访问引擎内部统一入口
    /// </summary>
    public class DataAccessEngine : IDataAccessEngine
    {
        private static object syncObj = new object();

        private static DataAccessEngine instance;
         

        /// <summary>
        /// 构造函数
        /// </summary>
        private DataAccessEngine()
        {

        }

        /// <summary>
        /// 获取数据访问引擎实例
        /// </summary>
        /// <returns>数据访问引擎实例</returns>
        public static DataAccessEngine GetInstance()
        {
            if (instance == null)
            {
                lock (syncObj)
                {
                    if (instance == null)
                    {
                        instance = new DataAccessEngine();
                    }
                }
            }

            return instance;
        }

        public ISaveService<CompositeDataObject> CreateCDOSaveService()
        {
            var service = new CDODataSaveService();
            InitServiceEvent(service);
            return service;
        }

        public IUpdateService<CompositeDataObject> CreateCDOUpdateService()
        {
            var service = new CDODataUpdateService();
            InitServiceEvent(service);
            return service;
        }

        public IDeleteService<CompositeDataObject> CreateCDODeleteService()
        {
            var service = new CDODataDeleteService();
            InitServiceEvent(service);
            return service;
        }

        public IQueryService<CompositeDataObject> CreateCDOQueryService()
        {
            var service = new CDODataQueryService();
            InitServiceEvent(service);
            return service;
        }

        public ISaveService<DataObject> CreateDOSaveService()
        {
            var service = new DOSaveService();
            InitServiceEvent(service);
            return service;
        }

        public IUpdateService<DataObject> CreateDOUpdateService()
        {
            var service = new DOUpdateService();
            InitServiceEvent(service);
            return service;
        }

        public IDeleteService<DataObject> CreateDODeleteService()
        {
            var service = new DODeleteService();
            InitServiceEvent(service);
            return service;
        }

        public IQueryService<DataObject> CreateDOQueryService()
        {
            var service = new DOQueryService();
            InitServiceEvent(service);
            return service;
        }

        public IModelQueryService<DataQuery> CreateDataQueryService()
        {
            var service = new DQQueryServiceProxy();
            InitServiceEvent(service);
            return service;
        }

        public CDOChangeSetService CreateChangeSetService()
        {
            return new CDOChangeSetService();
        }

        /// <summary>
        /// 初始化Service的事件入口
        /// </summary>
        /// <param name="dataAccessAtion"></param>
        private void InitServiceEvent(IDataAccessAtion dataAccessAtion)
        {
            //warning：需要确认每个插件的InvokeContext实例是否能保存到整个Service服务方法执行完毕之前；
            DataAccessPluginExecutor dataAccessPluginExecutor = new DataAccessPluginExecutor();
            dataAccessAtion.BuildContext += dataAccessPluginExecutor.BuildContext;
            dataAccessAtion.Execute += dataAccessPluginExecutor.Execute;
        }

        ISQLService IDataAccessEngine.CreateSQLService()
        {
            return new SQLService();
        }
    }
}
